package com.example.xiongmao.service.impl;

import com.example.xiongmao.constant.MessageConstant;
import com.example.xiongmao.constant.StatusConstant;
import com.example.xiongmao.entity.DTO.CommonStatus;
import com.example.xiongmao.entity.LoginDTO;
import com.example.xiongmao.entity.DTO.shopmanager.ShopManagerDTO;
import com.example.xiongmao.entity.DTO.shopmanager.addShopManagerDTO;
import com.example.xiongmao.entity.PageResult;
import com.example.xiongmao.entity.ShopManager;
import com.example.xiongmao.entity.VO.shopmanager.ShopManagerChargeVO;
import com.example.xiongmao.exception.AccountLockedException;
import com.example.xiongmao.exception.AccountNotFoundException;
import com.example.xiongmao.exception.PasswordErrorException;
import com.example.xiongmao.mapper.ShopManagerChargeMapper;
import com.example.xiongmao.service.ShopManagerChargeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service
public class ShopManagerChargeServiceImpl implements ShopManagerChargeService {

    @Autowired
    private ShopManagerChargeMapper shopManagerChargeMapper;

    @Override
    public PageResult list(ShopManagerDTO shopManagerDTO) {
        // 设置分页参数
        PageHelper.startPage(shopManagerDTO.getPage(), shopManagerDTO.getPageSize());

        // 调用mapper方法进行查询
        List<ShopManagerChargeVO> pageResults = shopManagerChargeMapper.list(shopManagerDTO);
        Page page = (Page) pageResults;
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增店长
     * @param addShopManager
     */
    @Override
    public void add(addShopManagerDTO addShopManager) {
        shopManagerChargeMapper.add(addShopManager);
    }

    @Override
    public ShopManagerChargeVO getbyId(Integer id) {
        ShopManagerChargeVO shopManager = shopManagerChargeMapper.getbyId(id);
        return shopManager;
    }

    @Override
    public void updateShopManager(addShopManagerDTO addShopManager) {
        shopManagerChargeMapper.updateShopManager(addShopManager);
    }

    @Override
    public void deleteShopManagerBatch(List<Integer> ids) {
        shopManagerChargeMapper.deleteShopManagerBatch(ids);
    }

    @Override
    public void shopManagerStatusChange(CommonStatus shopManagerStatus) {
        System.out.println("当前用户状态:" + shopManagerStatus.getStatus());
        if ("1".equals(shopManagerStatus.getStatus())) {
            // status==1表示正常，需要禁用==0
            shopManagerChargeMapper.shopManagerDisable(shopManagerStatus.getId());
        } else {
            // status==0表示禁用，需要正常==1
            shopManagerChargeMapper.shopManagerUNDisable(shopManagerStatus.getId());
        }
    }

    /**
     * 店长登录
     * @param LoginDTO
     * @return
     */
    @Override
    public ShopManager login(LoginDTO LoginDTO) {
        String username = LoginDTO.getUsername();
        String password = LoginDTO.getPassword();

        ShopManager shopManager = shopManagerChargeMapper.getByUserName(username);
        if (shopManager == null) {
            // 账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        //需要进行md5加密，然后再进行比对
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        System.out.println("当前被加密的密码：" + password);

        if (!password.equals(shopManager.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        if (shopManager.getStatus() == StatusConstant.DISABLE) {
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }
        return shopManager;
    }

}
